package com.kly.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kly.common.BaseResponse;
import com.kly.common.ResultUtils;
import com.kly.domain.dto.userchat.UserChatMessagePageRequest;
import com.kly.domain.entity.Message;
import com.kly.domain.vo.MessageVO;
import com.kly.service.MessageService;
import com.kly.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author admin
 * @date 2025/5/10
 */
@RequestMapping("/user/chat")
@RestController
@Slf4j
public class UserChatController {

    @Resource
    private MessageService messageService;

    @Resource
    private UserService userService;


    @GetMapping("/page")
    public BaseResponse<Page<MessageVO>> userChatMessagePage(UserChatMessagePageRequest userChatMessagePageRequest) {
        Page<Message> page = messageService.lambdaQuery()
                .eq(Message::getReceiverId, userChatMessagePageRequest.getGroupId())
                .eq(Message::getReceiverType, userChatMessagePageRequest.getReceiverType())
                .orderByDesc(Message::getCreateTime)
                .page(new Page<>(userChatMessagePageRequest.getCurrent(), userChatMessagePageRequest.getPageSize()));
        Page<MessageVO> messageVOPage = new Page<>();
        BeanUtil.copyProperties(page, messageVOPage);
        messageVOPage.setRecords(messageService.getMessageVOList(page.getRecords()));
        return ResultUtils.success(messageVOPage);
    }
}
